using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

namespace BitMango
{
	public static class Wallet
	{
		public static WalletInstance current
		{
			get
			{
				if (Wallet._current == null)
				{
					UnityEngine.Debug.LogError("Wallet.current is null");
				}
				return Wallet._current;
			}
			set
			{
				if (Wallet._current != null && Wallet._current.loaded)
				{
					Wallet._current.Save();
				}
				Wallet._current = value;
			}
		}

		public static bool IsValid()
		{
			return Wallet._current != null && Wallet._current.loaded;
		}

		//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public static event Action<InventoryItem, int> itemChangedCallback;

		//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public static event Action loadingDoneCallback;

		public static void FireItemChangedEvent(InventoryItem item, int count)
		{
			if (Wallet._current != null && Wallet.itemChangedCallback != null)
			{
				Wallet.itemChangedCallback(item, count);
			}
		}

		public static void FireLoadingDoneEvent()
		{
			if (Wallet._current != null && Wallet.loadingDoneCallback != null)
			{
				Wallet.loadingDoneCallback();
			}
		}

		public static bool Save()
		{
			return Wallet.current != null && Wallet.current.Save();
		}

		public static InventoryItem Get(string id)
		{
			if (Wallet.current != null)
			{
				return Wallet.current.Get(id);
			}
			return null;
		}

		public static int GetItemCount(string id)
		{
			if (Wallet.current != null)
			{
				return Wallet.current.GetItemCount(id);
			}
			return 0;
		}

		public static void Gain(ShopItem newItem, string where = null)
		{
			if (Wallet.current != null)
			{
				Wallet.current.Gain(newItem, where);
			}
		}

		public static void Gain(string inventoryItemId, int count, string where = null)
		{
			if (Wallet.current != null)
			{
				Wallet.current.Gain(inventoryItemId, count, where);
			}
		}

		public static bool Use(string id, int count = 1, string where = null)
		{
			return Wallet.current != null && Wallet.current.Use(id, count, where);
		}

		public static void Refund(ShopItem newItem)
		{
			if (Wallet.current != null)
			{
				Wallet.current.Refund(newItem);
			}
		}

		public static List<InventoryItem> inventory
		{
			get
			{
				if (Wallet.current != null)
				{
					return Wallet.current.inventory;
				}
				return null;
			}
		}

		public static bool hasNonConsumable
		{
			get
			{
				return Wallet.current != null && Wallet.current.hasNonConsumable;
			}
		}

		public static void MigrationFromBalanceToInventory(string inventoryItemId)
		{
			if (Wallet.current != null)
			{
				Wallet.current.MigrationFromBalanceToInventory(inventoryItemId);
			}
		}

		private static WalletInstance _current;
	}
}
